<template>
    <div class="form-item">
      <label>{{ label }}</label>
      <div class="input-wrapper">
        <!-- 日期选择器 -->
        <div v-if="type === 'date'" class="date-picker">
          <input
            type="text"
            :value="selectedDate"
            @click="showCalendar = !showCalendar"
            :placeholder="placeholder"
            readonly
          />
          <div class="calendar" v-if="showCalendar">
            <div class="calendar-header">
              <button @click="prevMonth">&lt;</button>
              <span>{{ currentYear }}年{{ currentMonth + 1 }}月</span>
              <button @click="nextMonth">&gt;</button>
            </div>
            <div class="calendar-body">
              <div class="weekdays">
                <span v-for="day in weekDays" :key="day">{{ day }}</span>
              </div>
              <div class="days">
                <span
                  v-for="(day, index) in calendarDays"
                  :key="index"
                  :class="{
                    'other-month': !day.currentMonth,
                    'selected': isSelected(day.date),
                    'today': isToday(day.date)
                  }"
                  @click="selectDate(day.date)"
                >
                  {{ day.day }}
                </span>
              </div>
            </div>
          </div>
        </div>

        <!-- 文本输入框 -->
        <input
          v-else-if="type === 'text'"
          type="text"
          :placeholder="placeholder"
        />
        
        <!-- 单选按钮组 -->
        <div v-else-if="type === 'radio'" class="radio-group">
          <label v-for="option in options" :key="option">
            <input type="radio" :name="label" :value="option">
            {{ option }}
          </label>
        </div>
        
        <!-- 复选框组 -->
        <div v-else-if="type === 'checkbox'" class="checkbox-group">
          <label v-for="option in options" :key="option">
            <input type="checkbox" :value="option">
            {{ option }}
          </label>
        </div>
        
        <!-- 下拉选择框 -->
        <select v-else-if="type === 'select'">
          <option v-for="option in options" :key="option" :value="option">
            {{ option }}
          </option>
        </select>
        
        <!-- 文本域 -->
        <textarea
          v-else-if="type === 'textarea'"
          :placeholder="placeholder"
        ></textarea>

        <!-- 数字输入框 -->
        <input 
          v-else-if="type === 'number'"
          type="text"
          :placeholder="placeholder"
          onkeyup="this.value=this.value.replace(/\D/g,'')" 
          onafterpaste="this.value=this.value.replace(/\D/g,'')"
        >

        <!-- 密码输入框 -->
        <input
          v-else-if="type === 'password'"
          type="password"
          :placeholder="placeholder"
        />
      </div>
    </div>
  </template>
  
  <script>
  export default {
    name: 'DynamicInput',
    props: {
      type: {
        type: String,
        required: true
      },
      label: {
        type: String,
        required: true
      },
      placeholder: {
        type: String,
        default: ''
      },
      options: {
        type: Array,
        default: () => []
      }
    },
    data() {
      return {
        showCalendar: false,
        currentDate: new Date(),
        selectedDate: '',
        weekDays: ['日', '一', '二', '三', '四', '五', '六']
      }
    },
    computed: {
      currentYear() {
        return this.currentDate.getFullYear()
      },
      currentMonth() {
        return this.currentDate.getMonth()
      },
      calendarDays() {
        const year = this.currentYear
        const month = this.currentMonth
        const firstDay = new Date(year, month, 1)
        const lastDay = new Date(year, month + 1, 0)
        const days = []

        // 添加上个月的日期
        const firstDayWeek = firstDay.getDay()
        for (let i = firstDayWeek - 1; i >= 0; i--) {
          const date = new Date(year, month, -i)
          days.push({
            day: date.getDate(),
            date: date,
            currentMonth: false
          })
        }

        // 添加当前月的日期
        for (let i = 1; i <= lastDay.getDate(); i++) {
          const date = new Date(year, month, i)
          days.push({
            day: i,
            date: date,
            currentMonth: true
          })
        }

        // 添加下个月的日期
        const remainingDays = 42 - days.length
        for (let i = 1; i <= remainingDays; i++) {
          const date = new Date(year, month + 1, i)
          days.push({
            day: i,
            date: date,
            currentMonth: false
          })
        }

        return days
      }
    },
    methods: {
      prevMonth() {
        this.currentDate = new Date(this.currentYear, this.currentMonth - 1, 1)
      },
      nextMonth() {
        this.currentDate = new Date(this.currentYear, this.currentMonth + 1, 1)
      },
      selectDate(date) {
        this.selectedDate = this.formatDate(date)
        this.showCalendar = false
      },
      isSelected(date) {
        if (!this.selectedDate) return false
        return this.formatDate(date) === this.selectedDate
      },
      isToday(date) {
        const today = new Date()
        return this.formatDate(date) === this.formatDate(today)
      },
      formatDate(date) {
        const year = date.getFullYear()
        const month = String(date.getMonth() + 1).padStart(2, '0')
        const day = String(date.getDate()).padStart(2, '0')
        return `${year}-${month}-${day}`
      }
    }
  }
  </script>
  
  <style scoped>
  .form-item {
    margin-bottom: 20px;
  }
  
  .form-item label {
    display: block;
    margin-bottom: 8px;
    font-weight: bold;
  }
  
  .input-wrapper {
    width: 100%;
  }
  
  input[type="text"],
  select,
  textarea {
    width: 100%;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
  }
  
  .radio-group,
  .checkbox-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
  
  .radio-group label,
  .checkbox-group label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: normal;
  }
  
  textarea {
    min-height: 100px;
    resize: vertical;
  }

  /* 日期选择器样式 */
  .date-picker {
    position: relative;
  }

  .calendar {
    position: absolute;
    top: 100%;
    left: 0;
    width: 300px;
    background: white;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    z-index: 1000;
    margin-top: 4px;
  }

  .calendar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    border-bottom: 1px solid #eee;
  }

  .calendar-header button {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px 8px;
    font-size: 16px;
  }

  .calendar-body {
    padding: 10px;
  }

  .weekdays {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    text-align: center;
    font-weight: 500;
    margin-bottom: 8px;
  }

  .days {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 4px;
  }

  .days span {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 32px;
    cursor: pointer;
    border-radius: 4px;
  }

  .days span:hover {
    background-color: #f0f0f0;
  }

  .days span.other-month {
    color: #ccc;
  }

  .days span.selected {
    background-color: #4CAF50;
    color: white;
  }

  .days span.today {
    border: 1px solid #4CAF50;
  }
  </style>